package com.qyhl.webtv.module_circle.circle.add;


import android.annotation.SuppressLint;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.launcher.ARouter;
import com.qyhl.webtv.basiclib.utils.DESedeUtil;
import com.qyhl.webtv.basiclib.utils.network.EasyHttp;
import com.qyhl.webtv.basiclib.utils.network.callback.SimpleCallBack;
import com.qyhl.webtv.basiclib.utils.network.exception.ApiException;
import com.qyhl.webtv.commonlib.common.CommonUtils;
import com.qyhl.webtv.commonlib.constant.ServicePathConstant;
import com.qyhl.webtv.commonlib.entity.UpTokenBean;
import com.qyhl.webtv.commonlib.entity.circle.CircleCheckTypeBean;
import com.qyhl.webtv.commonlib.entity.circle.CircleHomeBean;
import com.qyhl.webtv.commonlib.entity.circle.CirclePostBean;
import com.qyhl.webtv.commonlib.entity.intergral.CoinBean;
import com.qyhl.webtv.commonlib.service.IntergralService;
import com.qyhl.webtv.module_circle.common.CircleUrl;
import com.qyhl.webtv.module_circle.common.CircleUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * Created by helin on 2018/1/24.
 */

public class AddCircleModel implements AddCircleContract.AddCircleModel {


    @Autowired(name = ServicePathConstant.INTERGRAL_SERICE)
    public IntergralService intergralService;

    private final AddCircleContract.AddCirclePresenter presenter;

    AddCircleModel(AddCircleContract.AddCirclePresenter presenter) {
        this.presenter = presenter;
        ARouter.getInstance().inject(this);
    }


    @Override
    public void getTagList() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String time = sdf.format(new Date());
        String code = "Z5" + time + "I2863";
        String url = CircleUrl.CIRCLE_URL + "?timestamp=" + time;

        EasyHttp.post(url)
                .upBytes(DESedeUtil.encryptMode(code, "siteId=" + CommonUtils.getInstance().getSite() + "&method=tagList"))
                .isDecrypt(true)
                .decryptKet(code)
                .execute(new SimpleCallBack<List<CircleHomeBean.TagList>>() {
                    @Override
                    public void onError(ApiException e) {
                        presenter.getTagError();
                    }

                    @Override
                    public void onSuccess(List<CircleHomeBean.TagList> tagLists) {
                        presenter.getTagSuccess(tagLists);
                    }
                });
    }


    @Override
    public void postData(HashMap<String, String> params) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String time = sdf.format(new Date());
        String code = "Z5" + time + "I2863";
        String url = CircleUrl.CIRCLE_URL + "?timestamp=" + time;
        Iterator<Map.Entry<String, String>> iter = params.entrySet().iterator();
        StringBuilder result = new StringBuilder();
        while (iter.hasNext()) {
            Map.Entry<String, String> entry = iter.next();
            if (result.length() > 0) {
                result.append("&");
            }
            result.append(entry.getKey()).append("=").append(entry.getValue());
        }


        EasyHttp.post(url)
                .upBytes(DESedeUtil.encryptMode(code, result.toString()))
                .isDecrypt(true)
                .decryptKet(code)
                .execute(new SimpleCallBack<CirclePostBean>() {

                    @Override
                    public void onError(ApiException e) {
                        if (e.getCode() == 155) {
                            presenter.postError("提交失败，您已被管理员限制言论！");
                        } else {
                            presenter.postError(CircleUtils.getCircleMessage(e.getCode()));
                        }
                    }

                    @Override
                    public void onSuccess(CirclePostBean circlePostBean) {
                        presenter.postSuccess(circlePostBean.getTopicId());
                    }
                });
    }

    @Override
    public void getUpToken(final boolean isUp) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String time = sdf.format(new Date());
        String code = "Z5" + time + "I2863";
        String url = CircleUrl.CIRCLE_URL + "?timestamp=" + time;

        EasyHttp.post(url)
                .upBytes(DESedeUtil.encryptMode(code, "siteId=" + CommonUtils.getInstance().getSite() + "&method=getUpToken&type=2"))
                .isDecrypt(true)
                .decryptKet(code)
                .execute(new SimpleCallBack<UpTokenBean>() {
                    @Override

                    public void onError(ApiException e) {
                        presenter.getTokenError();
                    }

                    @Override
                    public void onSuccess(UpTokenBean upTokenBean) {
                        presenter.getTokenSuccess(upTokenBean, isUp);
                    }
                });

    }

    /**
     * 审核类型
     */
    @Override
    public void getCheckType(final boolean isUp) {
        @SuppressLint("SimpleDateFormat") SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String time = sdf.format(new Date());
        String code = "Z5" + time + "I2863";
        String url = CircleUrl.CIRCLE_URL + "?timestamp=" + time;

        EasyHttp.post(url)
                .upBytes(DESedeUtil.encryptMode(code, "siteId=" + CommonUtils.getInstance().getSite() + "&method=siteInfo"))
                .isDecrypt(true)
                .decryptKet(code)
                .execute(new SimpleCallBack<CircleCheckTypeBean>() {
                    @Override
                    public void onError(ApiException e) {
                        presenter.setCheckType(0, isUp);
                    }

                    @Override
                    public void onSuccess(CircleCheckTypeBean circleCheckTypeBean) {
                        presenter.setCheckType(circleCheckTypeBean.getMode(), isUp);
                    }
                });
    }

    @Override
    public void getCoin(String username) {
        intergralService.gainCoin("3", "gainCoin", new IntergralService.GainCoinCallBack() {
            @Override
            public void success(CoinBean coinBean) {
                presenter.getCoinSuccess(coinBean);
            }

            @Override
            public void error(String msg) {
                presenter.getCoinFailure(msg);
            }
        });


    }


}
